// pkgUser/pages/profile/profile.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    avatar: '',
    nickName: '',
    userInfo: {},
    show: '',
    mobile:'',
    address:'',
    select:'',
    notes:'',
    sex:1,
    columns: ['男', '女'],
    index:0,
    minDate: new Date().getTime(),
    currentDate: new Date().getTime(),
    birthday:'',
  },
  onInput(event) {
  //  console.log(event.detail);
   console.log(new Date(event.detail).toLocaleDateString());
   this.setData({birthday:new Date(event.detail).toLocaleDateString()})
  },
  showPopup(e) {
    console.log(e.mark.select);
    this.setData({ show:e.mark.select });
  },
  onChange({detail:{value}}){
    console.log(value);
    this.setData({index:value==='男'?0:1,sex:value==='男'?1:0})
    console.log(this.data.sex,this.data.index);
  },
  onClose() {
   
    this.setData({ show: false });
  },
  onClickRight({mark:{select}}){
    console.log(select);
    if(select==='mobile'){
    //   console.log(this.data.mobile);
      if(this.data.mobile.length !== 11 || isNaN(+this.data.mobile))return wx.toast('请输入正确手机号')
       wx.updateUserInfo({mobile:this.data.mobile,
        id:this.data.userInfo.id})
        this.onClose()
       this.setData({mobile:this.data.mobile})
    }else{
      console.log(this.data[select]);
      if(this.data[select]==='')return wx.toast(`请输入${select}`)
      const data = {id:this.data.userInfo.id}
      data[select] = this.data[select]
      wx.updateUserInfo(data)
        this.onClose()
        this.setData({[select]:this.data[select]})
    }
  },
  onShow () {
      this.getUserInfo() 
  },
  async getUserInfo () {
    const res = await wx.http.get('/user/getUserInfo')
    console.log(res);
    if (res.errno === 0) {
      this.setData({ userInfo: res.data.userInfo, avatar: res.data.userInfo.icon, nickName: res.data.userInfo.username,mobile:res.data.userInfo.mobile,address:res.data.userInfo.address,notes:res.data.userInfo.notes,sex:res.data.userInfo.sex,index:res.data.userInfo.sex===1?0:1,birthday:res.data.userInfo.birthday})
      getApp().userInfo = res.data
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad () {
  
  },
  getAvatar (e) {
    console.log(e.detail.avatarUrl);
    this.disposePic(e.detail.avatarUrl)
  },
  disposePic (avatar) {
    wx.uploadFile({
      url: wx.http.baseURL + '/common/upload?type=images',
      filePath: avatar,
      name: 'file',
      header: {
        Token: getApp().token
      },
      success: async (res) => {
        console.log(res);
        const { data } = JSON.parse(res.data)
        console.log(data);
        console.log(this.data.userId);
        const result = await wx.http.post('/user/update', { id: this.data.userInfo.id, icon: data.savePath }
        )
        console.log(result);
        this.getUserInfo()
      }
    })
  },

  getNickName (e) {
    console.log(e.detail.value);
    this.uploadNickname(e.detail.value)
  
  },
  async uploadNickname (Nickname) {
    const res = await wx.http.post('/user/update', { id: this.data.userInfo.id, username: Nickname })
    console.log(res);
    this.getUserInfo()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {

  },

  /**
   * 生命周期函数--监听页面显示
   */


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () {

  }
})